package servlet.server;

import servlet.Servlet;
import servlet.ServletImpl;
import servlet.http.Request;
import servlet.http.Response;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * Created by root on 16-10-20.
 */
public class Server2 {
    public void start(){
        try {
            ServerSocket serverSocket = new ServerSocket(8888);
            //2、接收来自浏览器的请求
            this.recevie(serverSocket);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private void recevie(ServerSocket serverSocket){
        try {
            Socket client = serverSocket.accept();
            Servlet servlet = new ServletImpl();

            Request request = new Request(client.getInputStream());
            Response response = new Response(client.getOutputStream());

            servlet.service(request, response);
            response.pushToClient(200);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        Server2 server = new Server2();
        //1、创建一个服务器端并开启
        server.start();
    }

}
